<template>
	<view class="container">
		<view class="main">
			<view v-if="!addresses.length" class="no-address-tips">
				<view class="mb-30">暂无地址信息</view>
				<view>请点击底部按钮添加地址信息</view>
			</view>
			<template v-else>
				<uni-swipe-action>
					<uni-swipe-action-item class="address-wrapper" :options="swipeOption" @click="handleSwipeClick(address.id)" v-for="(address, index) in addresses" :key="index">
						<view class="address" @tap="chooseAddress(address)">
							<view class="left flex-fill overflow-hidden mr-20">
								<view class="font-size-lg font-weight-bold text-truncate" style="margin-bottom: 10rpx;">{{ address.street }}</view>
								<view class="font-size-sm text-color-assist">
									{{ address.acceptName }} {{ !address.sex ? '先生' : '女士' }} {{ address.mobile }}
								</view>
							</view>
							<image src="/static/images/edit.png" class="edit-icon" @tap.stop="edit(address.id)"></image>
						</view>
					</uni-swipe-action-item>
				</uni-swipe-action>
			</template>
		</view>
		<view class="btn-box">
			<button type="primary" size="default" @tap="add">新增地址</button>
		</view>
	</view>
</template>

<script>
	import {mapState, mapMutations} from 'vuex'
	import uniSwipeAction from '../../components/uni-swipe-action/uni-swipe-action'
	import uniSwipeActionItem from '../../components/uni-swipe-action-item/uni-swipe-action-item'
	import $http from '@/common/api/request.js'
	
	export default {
		components: {
			uniSwipeAction,
			uniSwipeActionItem
		},
		data() {
			return {
				deleted_id:'',
				addresses: [], //所有地址
				scene: 'menu',
				is_choose: false, //是否选择地址
				swipeOption: [
					{
						text: '删除',
						style: {
							backgroundColor: '#D12E32'
						}
					}
				]
			}
		},
		computed: {
			...mapState(['addresses'])
		},
		async onLoad({is_choose, scene}) {
			this.is_choose = is_choose || false
			this.scene = scene || 'menu'
			this.init()
		},
		created() {
			uni.$on('updateAddress',this.init())
		},
		methods: {
			...mapMutations(['SET_ADDRESS', 'SET_ADDRESSES', 'SET_ORDER_TYPE']),
			init() {	//页面初始化
				this.loading = true
				$http.request({
						url:"/addresses/list",
						method:"GET",
					}).then((res)=>{
						if(res.code == 200){
							console.log("蓝湛，看我看我！addresses");
							this.addresses = res.data;
							this.SET_ADDRESS(this.addresses)
							console.log(this.addresses);	
						}else{
							uni.showToast({
								title:'地址数据获取失败，请重试',
								icon:'none'
							})
							}
					}).catch(()=>{
						uni.showToast({
							title:'/addresses/list接口获取失败，请重试',
							icon:'none'
						})
					})	
				// this.goods = await this.$api('goods')
				console.log("里面的addresses")
				console.log(this.addresses)
				console.log("宝贝来吧，addresses在上面")
				this.loading = false
			},
			add() {
				uni.navigateTo({
					url: '/pages/address/add'
				})
			},
			edit(id) {
				uni.navigateTo({
					url: '/pages/address/add?id=' + id
				})
			},
			handleSwipeClick(id) {
				this.deleted_id = "/addresses/"+id
				uni.showModal({
					title: '提示',
					content: '确定要删除？',
					success: res => {
						if(res.confirm) {
							//删除
							$http.request({
									url:this.deleted_id,
									method:"DELETE",
								}).then((res)=>{
									if(res.code == 200){
										uni.showToast({title: '删除成功！', icon: 'success'})
										
									}else{
										uni.showToast({
											title:'地址删除失败，请重试',
											icon:'none'
										})
										}
								}).catch(()=>{
									uni.showToast({
										title:'/{id}接口获取失败，请重试',
										icon:'none'
									})
								})	
							
							
							// const index = this.addresses.findIndex(item => item.id == id)
							// const addresses = JSON.parse(JSON.stringify(this.addresses))
							// addresses.splice(index, 1)
							// this.SET_ADDRESSES(addresses)
							
						}
					}
				})
			},
			chooseAddress(address) {
				if(!this.is_choose) return
				this.SET_ADDRESS(address)
				this.SET_ORDER_TYPE('takeout')
				if(this.scene == 'menu') {
					uni.switchTab({
						url: '/pages/menu/menu'
					})
				} else if(this.scene == 'pay') {
					uni.navigateTo({
						url: '/pages/pay/pay'
					})
				}
			}
		}
	}
</script>

<style lang="scss" scoped>
	.container {
		width: 100%;
		height: 100%;
	}
	
	.main {
		width: 100%;
		padding: 30rpx;
		display: flex;
		flex-direction: column;
		padding-bottom: 100rpx;
		
		.address-wrapper {
			margin-bottom: 30rpx;
		}
		
		.address {
			width: 100%;
			padding: 40rpx 30rpx;
			background-color: #FFFFFF;
			display: flex;
			justify-content: space-between;
			align-items: center;
			
			.right {
				flex: 1;
				overflow: hidden;
				display: flex;
				flex-direction: column;
			}
			
			.edit-icon {
				width: 50rpx;
				height: 50rpx;
				flex-shrink: 0;
			}
		}
	}
	
	.btn-box {
		height: 100rpx;
		background-color: #FFFFFF;
		box-shadow: 0 0 20rpx rgba($color: $text-color-assist, $alpha: 0.1);
		position: fixed;
		bottom: 0;
		left: 0;
		right: 0;
		padding: 10rpx 0;
		display: flex;
		align-items: center;
		justify-content: center;
		
		button {
			height: 80rpx;
			width: 80%;
			border-radius: 50rem !important;
			display: flex;
			align-items: center;
			justify-content: center;
		}
	}
</style>
